<template>
	<view class="assetMang">
		<view class="app-title ac">
			<view class="fl" @click="back">
				<image class="back" src="/static/img/back.png"></image>
			</view>资产管理22
		</view>
		<web-view v-if="url" ref="webView" class="webViewClass" :webview-styles="webviewStyles" :src="url"
			@message="getMessage"></web-view>
	</view>
</template>
<script>
	import {
		WORK_CONTROLLER
	} from "../../api/pms";
	import {
		mapState
	} from "vuex";
	import NFC from '../../utils/nfc.js'
	export default {
		data() {
			return {
				webviewStyles: {
					top: '66px'
				},
				curWebview: null,
				// url: '/hybrid/html/assMang.html'
				url: "",
				// url: "http://47.100.137.66:8974/webroot/decision/url/mobile/packagedApp/?appId=66b60d9e-9d26-dbd4-1d99-33aec8995f4b&fine_username=admin"
			}
		},
		computed: {
			...mapState([
				"userInfo"
			]),
		},
		watch: {},
		onReady() {},
		onLoad(options) {
		},
		onUnload() {

		},
		async mounted() {
			await this.getUrl()
			// #ifdef H5

			window.addEventListener("message", async (e) => {
				console.log('父组件收到数据了', e.data)
				//	this.passChangeNode(e.data)
				let param = e.data
				if (e.data.inputType == 'ScanningCode') {
					let result = await this.scanCode()
					this.passMessage({
						inputType: e.data.inputType,
						resultType: e.data.resultType,
						data: result,
						userName: this.$store.state.userInfo.name,
						userId: this.$store.state.userInfo.id
					})
				}
				if (e.data.inputType == "NFCIdentify") {
					let result = await this.testNFC()
					this.passMessage({
						inputType: e.data.inputType,
						resultType: e.data.resultType,
						data: result,
						userName: this.$store.state.userInfo.name,
						userId: this.$store.state.userInfo.id
					})
				}
			})
			// #endif
			// #ifdef APP-PLUS
			let _this = this
			uni.getSystemInfo({
				success(res) {
					console.log(res) //手机牌子
					console.log(res.model) //手机型号
					console.log(res.screenWidth) //屏幕宽度
					console.log(res.screenHeight) //屏幕高度
					let height = res.screenHeight
					let windownHeight = res.windowHeight
					let childHeight = windownHeight - 66
					setTimeout(() => {
						_this.curWebview = _this.$scope.$getAppWebview().children()[0]
						_this.curWebview.setStyle({
							height: childHeight + 'px'
							// height: "200px"
						})
						console.log("页面高度", childHeight + 'px', _this.curWebview.setStyle)
					}, 1000)
				}
			})
			// #endif
		},
		methods: {
			getLocationUtil() {
				return new Promise((resolve, reject) => {
					uni.showToast({
						title: "获取位置信息中...",
						icon: "none",
					});
					// #ifdef APP-PLUS
					uni.getLocation({
						type: 'gcj02',
						geocode: 'true',
						success: function(res) {
							resolve({
								longitude: res.longitude,
								latitude: res.latitude
							})
						},
						// 定位失败
						fail: function(error) {
							console.log(error)
							uni.showModal({
								title: '提示',
								content: error.errMsg,
							})
							reject(error)
						}
					})
					// #endif
				})
			},
			async getReturnLocation(param, param1) {
				console.log('param--param', param, param1)
				await this.fetch("Get", this.$store.state.baseUrl + WORK_CONTROLLER.Get_zc_JudgeInRaw, param).then(
					async (
						res) => {
						if (res.success && res.data.inRawLocation != "NO" && res.data.have == 'YES') {
							this.passMessage(param1)
						} else if (res.success && res.data.inRawLocation == "NO" && res.data.have == 'YES') {
							uni.showToast({
								title: "该标签所在地点不对",
								icon: "none",
							});
						} else if (res.success&&res.data.have == 'NO') {
							// uni.showToast({
							// 	title: '该标签不存在',
							// 	icon: 'none'
							// })
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							});
						}
					}).catch(error => {
					console.log(error)
				})
			},
			getUrl() {
				this.fetch(
					"GET",
					this.$store.state.baseUrl + WORK_CONTROLLER.Get_zc_getFanRuan_URL, {}, false
				).then((res) => {
					if (res?.success) {
						this.url = `${res.data}&tempTime=${new Date().getTime()}`
					}
				})
			},
			createUrl() {
				return this.url + '&tempTime=' + new Date().getTime()
			},
			scanCode() {
				let _this = this
				let code
				return new Promise((resolve, reject) => {
					uni.scanCode({
						success: function(res) {
							console.log('设备扫码---', res)
							code = res.result
							resolve(res.result)
						}
					});
				})

			},
			async testNFC() {
				//这里用异步获取读取到的NFC数据
				const nfcId = await NFC.listenNFCStatus();
				// uni.showToast({
				// 	icon: "none",
				// 	title: nfcId,
				// })
				console.log(nfcId)
				if (!nfcId) {
					return ''
				} else {
					return nfcId
				}

			},
			passMessage(param) {
				// #ifdef APP-PLUS
				setTimeout(() => {
					this.curWebview = this.$scope.$getAppWebview().children()[0]
					this.curWebview.evalJS(`uniToFanRuanEvent(${JSON.stringify(param)})`)
				}, 500)
				// #endif
			},
			back() {
				uni.redirectTo({
					url: "/pages/index/index"
				})
			},
			async getMessage(e) {
				console.log('接收到消息了---1111--', JSON.stringify(e.detail.data[0]))
				let passValue = e.detail.data[0].message
				let node = passValue
				if (node.inputType == 'ScanningCode') {
					let result = await this.scanCode()
					let loPram = await this.getLocationUtil().then(resLocal => {
						let params = {
							longitude: resLocal.longitude,
							latitude: resLocal.latitude,
							zcCode: result
						}
						return params
					})
					await this.getReturnLocation(loPram, {
						inputType: node.inputType,
						resultType: node.resultType,
						data: result,
						userName: this.$store.state.userInfo.name,
						userId: this.$store.state.userInfo.id
					})
				}
				if (node.inputType == "NFCIdentify") {
					let result = await this.testNFC()
					
					if(!result)return 
					let loPram = await this.getLocationUtil().then(resLocal => {
						let params = {
							longitude: resLocal.longitude,
							latitude: resLocal.latitude,
							zcCode: result
						}
						return params
					})
					await this.getReturnLocation(loPram, {
						inputType: node.inputType,
						resultType: node.resultType,
						data: result,
						userName: this.$store.state.userInfo.name,
						userId: this.$store.state.userInfo.id
					})
				}
			},
		}
	}
</script>

<style lang="less" scoped>
	.assetMang {}

	.webViewClass {
		height: calc(100vh - 66px);
		width: 100vw;
		top: 66px !important;
		padding-bottom: var(--window-bottom);
	}
</style>
<style>
	* {
		/* 取消点击背景色显示蓝色 */
		-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
	}
</style>